/**
 * 
 */
package com.zufangbao.earth.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.InfoMessage;
import com.demo2do.core.web.resolver.Preloading;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.entity.company.corp.AppServiceConfig;
import com.zufangbao.sun.service.AppService;

/**
 * @author lute
 *
 */

@Controller
@RequestMapping("/apps/{appId}/service-configs")
public class AppServiceConfigController {
	
	@Autowired
	private AppService appService;
	
	/**
	 * Prepare to edit app service config
	 * 
	 * @param appId
	 * @return
	 */
	@RequestMapping("/{configId}/edit")
	public ModelAndView editServiceConfig(@PathVariable("appId") Long appId, @PathVariable("configId") Long configId) {
		
		ModelAndView modelAndView = new ModelAndView("app/service/service-config-edit");
		
		App app = appService.load(App.class, appId);
		AppServiceConfig appServiceConfig = appService.loadAppServiceConfig(configId);
		
		modelAndView.addObject("appServiceConfig", appServiceConfig);
		modelAndView.addObject("app", app);
		
		return modelAndView;
	}
	
	/**
	 * Edit app service config
	 * 
	 * @param appId
	 * @return
	 */
	@RequestMapping(value = "/{configId}", method = RequestMethod.PATCH)
	@InfoMessage("info.app-service-config.edit.success")
	public String onEditServiceConfig(@PathVariable("appId") Long appId, @Preloading AppServiceConfig appServiceConfig) {
		
		appService.updateAppServiceConfig(appServiceConfig);

		return "redirect:/apps/" + appId + "/config?channel=" + appServiceConfig.getChannel().getDescription();
	}

}
